#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@Author:    Colin
@Github:    https://github.com/opscolin
@Gitee:     https://gitee.com/colin5063
@DateTime:  2022/9
@File:      config.py
@Software:  PyCharm
"""

import utils
from prometheus_client import Gauge, push_to_gateway
from config import ALI_AK, ALI_SK
from aliyunapi import AliyunAPI


aliyun = AliyunAPI(ALI_AK, ALI_SK)


# -------------------------- 采集阿里云RDS 监控数据 -------------------------- #
def metric_rds(instance_name, registry):
    instance_id = utils.get_instance_id(instance_name)
    cpu, memory = aliyun.rds_cpu_memory_metric(instance_id)
    iops = aliyun.rds_iops_metric(instance_id)
    
    g_cpu = Gauge('mysql_cpu_usage', 'mysql cpu 使用率', ['instance_name'], registry=registry)
    g_cpu.labels(instance_name=instance_name).set(float(cpu))

    g_memory = Gauge('mysql_memory_usage', 'mysql memory 使用率', ['instance_name'], registry=registry)
    g_memory.labels(instance_name=instance_name).set(float(memory))

    g_iops = Gauge('mysql_global_iops', 'mysql iops', ['instance_name'], registry=registry)
    g_iops.labels(instance_name=instance_name).set(float(iops))

    push_to_gateway('192.168.3.108:9091', job=f'metric_rds_{instance_name}' , registry=registry)

def metric_rds_cpu_mem(instance_name, registry):
    instance_id = utils.get_instance_id(instance_name)
    cpu, memory = aliyun.rds_cpu_memory_metric(instance_id)

    g_cpu = Gauge('mysql_cpu_usage', 'mysql cpu 使用率', ['instance_name'], registry=registry)
    g_cpu.labels(instance_name=instance_name).set(float(cpu))

    g_memory = Gauge('mysql_memory_usage', 'mysql memory 使用率', ['instance_name'], registry=registry)
    g_memory.labels(instance_name=instance_name).set(float(memory))

    push_to_gateway('192.168.3.108:9091', job=f'mysql_cpu_memory_{instance_name}' , registry=registry)


def metric_rds_iops(instance_name, registry):
    instance_id = utils.get_instance_id(instance_name)
    iops = aliyun.rds_iops_metric(instance_id)
    g_iops = Gauge('mysql_global_iops', 'mysql iops', ['instance_name'], registry=registry)
    g_iops.labels(instance_name=instance_name).set(float(iops))
    push_to_gateway('192.168.3.108:9091', job=f"mysql_iops_{instance_name}",  registry=registry)


def metric_rds_dbsize(instance_name, dbname, registry):
    size = utils.get_mysql_dbsize(instance_name, dbname)
    gauge = Gauge('mysql_database_dbsize', "Database Dbsize", ['instance_name', 'dbname', 'instance'], registry=registry)
    gauge.labels(instance_name=instance_name, dbname=dbname, instance=dbname).set(float(size))
    push_to_gateway('192.168.3.108:9091', job=f'mysql_dbsize_{instance_name}_{dbname}', registry=registry)


def metric_rds_tablesize(instance_name, dbname, tablename, registry):
    size = utils.get_mysql_tablesize(instance_name, dbname, tablename)
    gauge = Gauge('mysql_database_tablesize', "Database Tablesize",  ['instance_name', 'dbname', 'tablename'], registry=registry)
    gauge.labels(instance_name=instance_name, dbname=dbname, tablename=tablename).set(float(size))
    push_to_gateway('192.168.3.108:9091', job=f'mysql_tablesize_{instance_name}_{dbname}', registry=registry)


def metric_rds_disk_usage(instance_name , registry):
    instance_id = utils.get_instance_id(instance_name)
    total = aliyun.rds_disk_size(instance_id)
    used = aliyun.rds_disk_used_size(instance_id)
    gauge_total = Gauge('mysql_disk_total_size', 'mysql实例磁盘总大小', ['instance_name'], registry=registry)
    gauge_total.labels(instance_name=instance_name).set(float(total))

    gauge_used = Gauge('mysql_disk_used_size', 'mysql实例磁盘已使用大小', ['instance_name'], registry=registry)
    gauge_used.labels(instance_name=instance_name).set(float(used))

    push_to_gateway('192.168.3.108:9091', job=f'mysql_disk_{instance_name}', registry=registry)
